<template>
  <div class="header">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>课程管理</el-breadcrumb-item>
      <!-- <el-breadcrumb-item>活动列表</el-breadcrumb-item>
      <el-breadcrumb-item>活动详情</el-breadcrumb-item> -->
    </el-breadcrumb>
    <el-dropdown>
      <span class="el-dropdown-link">
        <el-avatar
          shape="square"
          :size="40"
          :src="userInfo.portrait || require('@/assets/default-avatar.png')"
        ></el-avatar>
        <i class="el-icon-arrow-down el-icon--right"></i>
      </span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item>{{ userInfo.userName }}</el-dropdown-item>
        <el-dropdown-item
          divided
          @click.native="handleLogout"
        >退出</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
  </div>
</template>

<script lang="ts">
import { Vue, Component } from 'vue-property-decorator'
import { ACCESS_TOKEN, USER_INFO } from '@/utils/constants'
import { clearStorage, getStorage, setStorage } from '@/utils/storage'
import { getUserInfo } from '@/services/front'
import { clearCookie } from '@/utils/cookie'

@Component
export default class AppHeader extends Vue {
  private userInfo:any = {};

  async loadUserInfo () {
    try {
      if (getStorage(USER_INFO)) {
        this.userInfo = JSON.parse(getStorage(USER_INFO))
      } else {
        const data: any = await getUserInfo()
        this.userInfo = data.content
        setStorage(USER_INFO, JSON.stringify(data.content))
      }
      await this.$store.dispatch('user/setUserInfoAction', this.userInfo)
    } catch (e) {
      console.error(e)
    }
    console.log(this.userInfo)
  }

  handleLogout () {
    this.$confirm('确认退出吗？', '退出提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => { // 确认执行这里
      // 清除登录状态
      this.$store.dispatch('user/setUserInfoAction', null)
      clearCookie(ACCESS_TOKEN)
      clearStorage()

      // 跳转到登录页面
      this.$router.push({
        name: 'login'
      })

      this.$message({
        type: 'success',
        message: '退出成功!'
      })
    }).catch(() => { // 取消执行这里
      this.$message({
        type: 'info',
        message: '已取消退出'
      })
    })
  }

  async created () {
    await this.loadUserInfo()
  }
}
</script>

<style scoped lang="scss">
.header {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .el-dropdown-link {
    display: flex;
    align-items: center;
  }
}
</style>
